package com.sxfq.loansupermarket.backsystem.modular.theTest.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.constant.tips.Tip;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.AnswerListInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.SearchTestingInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.UserForTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.service.IAnswerListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 答题
 *
 * @author py
 * @Date 2018年2月4日23:05:54
 */
@Controller
@RequestMapping("/answerList")
public class AnswerListController extends BaseController {

    @Autowired
    IAnswerListService iAnswerListService;
    @Autowired
    private SqlMapper sqlMapper;

    /**
     * 查詢考试提列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(SearchTestingInfo query) throws Exception {
        Page<AnswerListInfo> page = new PageFactory<AnswerListInfo>().defaultPage();
        try {
            query.setUserForTestId(ShiroKit.getSessionAttr("userForTestId"));
            List<AnswerListInfo> testQuestionsAnswerLisInfoList = iAnswerListService.selectPageAnswerListByQueryGetMap(page, query);
            page.setRecords(testQuestionsAnswerLisInfoList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     * 修改考试
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Tip update(@Valid UserForTestInfo entity, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        iAnswerListService.UpdateWithTheInfo(entity);
        return SUCCESS_TIP;
    }

    /**
     * 修改当前页
     */
    @RequestMapping(value = "/updatesCurrentPage")
    @ResponseBody
    public Tip updatesCurrentPage() throws Exception {
        String userId = super.getPara("userId").trim();
        String theTestId = super.getPara("theTestId").trim();
        String currentPage = super.getPara("currentPage").trim();
        String sql = "update user_for_test_info set current_page=" + currentPage + " where user_id=" + userId + " and the_test_id='" +
                "" + theTestId + "'";
        sqlMapper.update(sql);
        return SUCCESS_TIP;
    }

    /**
     * 修改考试
     */
    @RequestMapping(value = "/updates")
    @ResponseBody
    public Tip updates() throws Exception {
        String id = super.getPara("id").trim();
        String userAnswer = super.getPara("userAnswer").trim();
        String sql = "select b.correct_answer from user_for_test_info a left join test_questions_info b on a.test_questions_id=b.id " +
                "where a.id='" + id + "' ";
        Map<String, Object> correct_answer = sqlMapper.selectOne(sql);
        String correctAnswer = correct_answer.get("correct_answer").toString();
        UserForTestInfo entity = new UserForTestInfo();
        entity.setId(id);
        entity.setUserAnswer(userAnswer);
        if (userAnswer.equals(correctAnswer)) {
            entity.setIsRight(1);
        } else {
            entity.setIsRight(2);
        }
        iAnswerListService.UpdateWithTheInfo(entity);
        return SUCCESS_TIP;
    }
}
